//
//  Base.m
//  Gfbase
//
//  Created by 谷丰 on 2025/3/24.
//

#import "BaseWay.h"
@implementation BaseWay
//域名
+(NSString *)BasePostUrlWith:(NSString *)index{
    return [NSString stringWithFormat:@"https://api.jiehuiyuan.com/%@",index] ;
}
+ (instancetype)sharedManager {
    static BaseWay *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[BaseWay alloc] init];
    });
    return instance;
}

// 普通 POST 请求
+ (void)postWithURL:(NSString *)url
         parameters:(id)parameters
            success:(void (^)(id response))success
            failure:(void (^)(NSError *error))failure {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager POST:url parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

// 普通 GET 请求
+ (void)getWithURL:(NSString *)url
        parameters:(id)parameters
           success:(void (^)(id response))success
           failure:(void (^)(NSError *error))failure {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager GET:url parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

// 上传图片
+ (void)uploadImageWithURL:(NSString *)url
                parameters:(id)parameters
                    images:(NSArray<UIImage *> *)images
                   success:(void (^)(id response))success
                   failure:(void (^)(NSError *error))failure {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager POST:url parameters:parameters headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (UIImage *image in images) {
            NSData *imageData = UIImageJPEGRepresentation(image, 0.8);
            [formData appendPartWithFileData:imageData name:@"file" fileName:@"image.jpg" mimeType:@"image/jpeg"];
        }
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

// 上传视频
+ (void)uploadVideoWithURL:(NSString *)url
                parameters:(id)parameters
                  videoURL:(NSURL *)videoURL
                   success:(void (^)(id response))success
                   failure:(void (^)(NSError *error))failure {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager POST:url parameters:parameters headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
        [formData appendPartWithFileData:videoData name:@"file" fileName:@"video.mp4" mimeType:@"video/mp4"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}
+ (void)saveToken:(NSString *)token {
    if (token) {
        [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"TokenKey"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

+ (NSString *)getToken {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"TokenKey"];
}

+ (void)clearToken {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"TokenKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
// Accid 相关方法
+ (void)saveAccid:(NSString *)Accid{
    if (Accid) {
        [[NSUserDefaults standardUserDefaults] setObject:Accid forKey:@"Accid"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}
+ (NSString *)getAccid{
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"Accid"];
}
+ (void)clearAccid{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Accid"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

// IMToken 相关方法
+ (void)saveIMToken:(NSString *)IMToken{
    if (IMToken) {
        [[NSUserDefaults standardUserDefaults] setObject:IMToken forKey:@"IMToken"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}
+ (NSString *)getIMToken{
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"IMToken"];
}
+ (void)clearIMToken{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"IMToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
#pragma mark + UserID 相关方法

+ (void)saveUserID:(NSString *)userID {
    if (userID) {
        [[NSUserDefaults standardUserDefaults] setObject:userID forKey:@"UserIDKey"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

+ (NSString *)getUserID {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"UserIDKey"];
}

+ (void)clearUserID {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"UserIDKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma mark + 是否第一次安装 APP

+ (BOOL)isFirstLaunch {
    // 如果 key 不存在，则认为是第一次安装
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunchKey"]) {
        return YES;
    }
    return NO;
}

+ (void)setFirstLaunchCompleted {
    // 标记为已经完成第一次启动
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstLaunchKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
//语言存储本地
+(void)FillLangue:(NSString *)Langue{
    [[NSUserDefaults standardUserDefaults]setObject:Langue forKey:@"Langue"];
}
+(NSString *)Langue{
    
    
    NSString * la =[[NSUserDefaults standardUserDefaults] objectForKey:@"Langue"];
    if ([BaseWay isNullPoint:la]) {
        la =[NSBundle.mainBundle preferredLocalizations][0];
    }
    return  la;
}
+(NSString *)LangueWithString:(NSString *)str{
//    [BaseWay FillGFstringArray:str];
    NSString * la =[[NSUserDefaults standardUserDefaults] objectForKey:@"Langue"];
    if ([BaseWay isNullPoint:la]) {
        la =[NSBundle.mainBundle preferredLocalizations][0];
    }
    return  la;
}
+(NSMutableArray *)GFstringArray{
    return  [[NSUserDefaults standardUserDefaults] objectForKey:@"GFstringArray"];
}

//中英文存储
+(void)FillGFstringArray:(NSString *)HistoryText{
    [[NSUserDefaults standardUserDefaults]setObject:[BaseWay GfstringArrayWithString:HistoryText andWithArray:[BaseWay GFstringArray]]  forKey:@"GFstringArray"];
}
+(NSArray * )GfstringArrayWithString:(NSString *)str andWithArray:(NSArray *)array{
    NSMutableArray * MuArray= [[NSMutableArray alloc]init];
    if(array.count>0){
        MuArray= [NSMutableArray arrayWithArray:array];
    }
    int index = 99;
    for (int x=0; x<MuArray.count; x++) {
        if([str isEqualToString:MuArray[x]]){
            index=x;
        }
    }
    if(index==99){
        [MuArray addObject:str];
    }
    return  MuArray;
}
//判断为空
+(BOOL)isNullPoint:(NSObject*)obj{
    if (obj==nil) {
        return YES;
    }else if ([obj isEqual:[NSNull null]]){
        return YES;
    }else if([obj isEqual:@""]){
        return YES;
    }
    return NO;
}
/*
 主题色
 */
+(UIColor *)MainColor{
    
    return rgba(7, 193, 96, 1);
}
+ (NSString *)currentLanguage {
    NSString *userLanguage = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserLanguageKey"];
    if (!userLanguage) {
        // 如果没有设置过语言，使用系统语言
        userLanguage = [[NSLocale preferredLanguages] firstObject];
        if ([userLanguage hasPrefix:@"zh"]) {
            userLanguage = @"zh-Hans"; // 简体中文
        } else {
            userLanguage = @"en"; // 默认英文
        }
    }
    return userLanguage;
}

+ (void)setLanguage:(NSString *)language {
    [[NSUserDefaults standardUserDefaults] setObject:language forKey:@"UserLanguageKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    // 发送语言改变通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"LanguageDidChange" object:nil];
}

+ (NSString *)localizedStringForKey:(NSString *)key {
    NSString *language = [self currentLanguage];
    NSString * Path  =[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
    
    NSString * Languagestring = NSLocalizedStringFromTableInBundle(key, @"Localizable",[NSBundle bundleWithPath:Path] , nil);
   
   
    return Languagestring;
}

//设定textfiledleftview
+(void)Textfiled:(UITextField *)textfiled AndViewWithImage:(NSString *)imageName andWithType:(NSString * )type{
    
    UIView * AlowayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, FitSizeW(50), textfiled.frame.size.height)];
    AlowayView.backgroundColor=[UIColor clearColor];
    UIImageView* ImageView = [[UIImageView alloc]initWithFrame:CGRectMake(FitSizeW(21), FitSizeW(11), FitSizeW(20), FitSizeW(20))];
    ImageView.image =[UIImage imageNamed:imageName];
    [AlowayView addSubview:ImageView];
    if ([type isEqualToString:@"right"]) {
        textfiled.rightViewMode=UITextFieldViewModeAlways;
        textfiled.rightView = AlowayView;
    }else{
        textfiled.leftViewMode=UITextFieldViewModeAlways;
        textfiled.leftView = AlowayView;
    }
}

@end
